package sz.szsmk.citizencard.ui.dialogs

import android.app.Dialog
import android.content.Context
import android.graphics.BitmapFactory
import android.graphics.Color
import android.os.Bundle
import android.text.SpannableString
import android.text.Spanned
import android.text.TextPaint
import android.text.method.LinkMovementMethod
import android.text.style.ClickableSpan
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.CompoundButton
import android.widget.TextView
import android.widget.Toast
import com.google.gson.internal.`$Gson$Preconditions`
import kotlinx.android.synthetic.main.dialog_mywallet_new.*
import sz.szsmk.citizencard.R
import sz.szsmk.citizencard.model.network.bean.response.ProtocolsRes
import sz.szsmk.citizencard.util.Constant
import sz.szsmk.citizencard.util.JumpUtils

/**
 * author : clark
 * date : 2020/11/27
 * description :
 */
class WalletAuthDialogNew(context: Context, type: String, mProtocolsRes: ProtocolsRes, mIsFirst: String) : Dialog(context, R.style.alertdialog) {

    var iWalletAuthDialog: IWalletAuthDialogNew? = null
    lateinit var protocolText: TextView
    var dialogType: String? = type
    var protocolsRes: ProtocolsRes = mProtocolsRes

    //参数1 代表是首次进入  参数0代表有新的协议更新
    var isFirst: String? = mIsFirst
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(LayoutInflater.from(context).inflate(R.layout.dialog_mywallet_new, null))
        val divierId = context.resources.getIdentifier("android:id/titleDivider", null, null)
        val divider = findViewById<View>(divierId)
        divider.visibility = View.GONE
        initView()
    }

    private fun initView() {
        //钱包弹窗      授权我的钱包获取以下信息
        //市民金融弹窗  授权市民金融获取以下信息
        protocolText = protocol_content


        when (dialogType) {
            //钱包弹窗UI
            "wallet" -> {
                when (isFirst) {
                    "1" -> protocolText.setText(context.resources.getString(R.string.agree))
                    "0" -> protocolText.setText(context.resources.getString(R.string.dialog_tips_start))
                }
                dialog_img.setImageBitmap(BitmapFactory.decodeResource(context.resources, context.resources.getIdentifier("ic_wodeqianbao", "mipmap", context.getPackageName())))
                dialog_title.text = context.resources.getString(R.string.my_wallet)
                dialog_tips.text = String.format(context.resources.getString(R.string.dialog_tips), context.resources.getString(R.string.my_wallet))

                for ((index, element) in protocolsRes.userAppAgreementDtos.withIndex()) {
                    val spStr: SpannableString
                    spStr = if (index == protocolsRes.userAppAgreementDtos.size - 1) {
                        SpannableString(element.agreementName)  //上下一样，文案有修改不一致的话可以修改
                    } else {
                        SpannableString(element.agreementName)
                    }
                    val protocol = element.agreementName
                    val clickSpan = ProtocolDialog.NoLineClickSpan("#2184FA", protocolsRes.userAppAgreementDtos[index].h5Url, context) //设置超链接
                    spStr.setSpan(clickSpan, 0, protocol.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
                    protocolText.append(spStr)
                }

                when (isFirst) {
                    "1" -> protocolText.append("")
                    "0" -> protocolText.append(String.format(context.resources.getString(R.string.dialog_tips_end), context.resources.getString(R.string.my_wallet)))
                }


            }
            //市民金融弹窗UI
            "finance" -> {
                when (isFirst) {
                    "1" -> protocolText.setText(context.resources.getString(R.string.agree))
                    "0" -> protocolText.setText(context.resources.getString(R.string.new_version))
                }
                dialog_img.setImageBitmap(BitmapFactory.decodeResource(context.resources, context.resources.getIdentifier("ic_jinrong", "mipmap", context.getPackageName())))
                dialog_title.text = context.resources.getString(R.string.finance)
                dialog_tips.text = String.format(context.resources.getString(R.string.dialog_tips), context.resources.getString(R.string.finance))
                for ((index, element) in protocolsRes.userAppAgreementDtos.withIndex()) {
                    val spStr: SpannableString
                    spStr = if (index == protocolsRes.userAppAgreementDtos.size - 1) {
                        SpannableString(element.agreementName)  //上下一样，文案有修改不一致的话可以修改
                    } else {
                        SpannableString(element.agreementName)
                    }
                    val protocol = element.agreementName
                    val clickSpan = ProtocolDialog.NoLineClickSpan("#2184FA", protocolsRes.userAppAgreementDtos[index].h5Url, context) //设置超链接
                    spStr.setSpan(clickSpan, 0, protocol.length, Spanned.SPAN_INCLUSIVE_EXCLUSIVE)
                    protocolText.append(spStr)
                }
                when (isFirst) {
                    "1" -> protocolText.append("")
                    "0" -> protocolText.append(String.format(context.resources.getString(R.string.dialog_tips_end), context.resources.getString(R.string.finance)))
                }


            }
        }
        protocolText.setMovementMethod(LinkMovementMethod.getInstance())
        iv_close?.setOnClickListener { v: View? -> dismiss() }
        btn_cancel?.setOnClickListener { v: View -> dismiss() }
        cb_authorization?.setOnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean -> btn_ok?.isEnabled = isChecked }
        btn_ok?.setOnClickListener { v: View? ->
            if (iWalletAuthDialog != null) {
                if (cb_authorization.isChecked) {
                    iWalletAuthDialog?.onclick()
                    dismiss()
                } else {
                    Toast.makeText(context, "请勾选协议", Toast.LENGTH_LONG).show()
                }
            }
        }
    }

    fun setiWalletAuthDialog(iWalletAuthDialog: IWalletAuthDialogNew?) {
        if (iWalletAuthDialog != null) {
            this.iWalletAuthDialog = iWalletAuthDialog
        }
    }

    interface IWalletAuthDialogNew {
        fun onclick()
    }

    //去除默认的clickSpan的下划线
    internal class NoLineClickSpan(var color: String, mUrl: String, context: Context) : ClickableSpan() {
        var context: Context = context;
        var url:String=mUrl;
        override fun updateDrawState(ds: TextPaint) {
            //设置字体颜色
            ds.color = (Color.parseColor(color))
            ds.isUnderlineText = false //去掉下划线
        }

        override fun onClick(widget: View) {
            //点击超链接时调用
            val bundle = Bundle()
            bundle.putString("url", url)
            JumpUtils.jumptoNatvie(context, "webView", 0, bundle)
        }
    }

}